/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.alxbrendmaquinarias.model;

import com.alxbrendmaquinarias.util.VarUtil;
import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author Max
 */
@Entity
@Table(name = "input_document", catalog = "sgcom", schema = "")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "InputDocument.findAll", query = "SELECT i FROM InputDocument i"),
    @NamedQuery(name = "InputDocument.findAllID", query = "SELECT NEW com.alxbrendmaquinarias.model.InputDocument(i.id, i.inputDate,i.numberDocument,i.total,i.typeDocument,i.typeMoney) from InputDocument i ORDER BY i.inputDate DESC"),
    @NamedQuery(name = "InputDocument.findAllIDDate", query = "SELECT NEW com.alxbrendmaquinarias.model.InputDocument(i.id, i.inputDate,i.numberDocument,i.total,i.typeDocument,i.typeMoney) from InputDocument i WHERE DATE(i.inputDate) = DATE(:date) ORDER BY i.inputDate DESC"),
    @NamedQuery(name = "InputDocument.findAllIDNumber", query = "SELECT NEW com.alxbrendmaquinarias.model.InputDocument(i.id, i.inputDate,i.numberDocument,i.total,i.typeDocument,i.typeMoney) from InputDocument i WHERE i.numberDocument = :number ORDER BY i.inputDate DESC"),
    @NamedQuery(name = "InputDocument.findAllIDDateNumber", query = "SELECT NEW com.alxbrendmaquinarias.model.InputDocument(i.id, i.inputDate,i.numberDocument,i.total,i.typeDocument,i.typeMoney) from InputDocument i WHERE DATE(i.inputDate) = DATE(:date) AND i.numberDocument = :number ORDER BY i.inputDate DESC"),
    @NamedQuery(name = "InputDocument.findById", query = "SELECT i FROM InputDocument i WHERE i.id = :id"),
    @NamedQuery(name = "InputDocument.findByInputDate", query = "SELECT i FROM InputDocument i WHERE i.inputDate = :inputDate"),
    @NamedQuery(name = "InputDocument.findByNumberDocument", query = "SELECT i FROM InputDocument i WHERE i.numberDocument = :numberDocument"),
    @NamedQuery(name = "InputDocument.findByTypeDocument", query = "SELECT i FROM InputDocument i WHERE i.typeDocument = :typeDocument"),
    @NamedQuery(name = "InputDocument.findByTypeMoney", query = "SELECT i FROM InputDocument i WHERE i.typeMoney = :typeMoney"),
    @NamedQuery(name = "InputDocument.findByDescription", query = "SELECT i FROM InputDocument i WHERE i.description = :description")})
public class InputDocument implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Column(name = "input_date")
    @Temporal(TemporalType.TIMESTAMP)
    private Date inputDate = new Date();
    @Size(max = 5)
    @Column(name = "number_document")
    private String numberDocument;
    @Size(max = 45)
    @Column(name = "type_document")
    private String typeDocument;
    @Size(max = 45)
    @Column(name = "type_money")
    private String typeMoney;
    @Size(max = 200)
    @Column(name = "description")
    private String description;
    @Column(name = "sub_total")
    private Double subTotal;
    @Column(name = "total")
    private Double total = 0.0;
    @Column(name = "igv")
    private Double igv = 18.0;
    @JoinColumn(name = "employee_id", referencedColumnName = "id")
    @ManyToOne
    private Employee employeeId;
    @JoinColumn(name = "provider_id", referencedColumnName = "id")
    @ManyToOne
    private Provider providerId;
    @OneToMany(mappedBy = "inputDocumentId")
    private List<AccountForPay> accountForPayList;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "inputDocumentId")
    private List<DetailInputDocument> detailInputDocumentList;

    public InputDocument() {
    }

    public InputDocument(Integer id, Date inputDate, String numberDocument, Double total, String typeDocument, String typeMoney) {
        this.id = id;
        this.inputDate = inputDate;
        this.numberDocument = numberDocument;
        this.total = total;
        this.typeDocument = typeDocument;
        this.typeMoney = typeMoney;
    }

    public InputDocument(Integer id) {
        this.id = id;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Date getInputDate() {
        return inputDate;
    }
    
    public boolean getEqualsDateToday() {
        if(inputDate == null ) {
            return false;
        } else {
            Calendar cal = Calendar.getInstance();
            cal.setTime(inputDate);
            Calendar cal2 = Calendar.getInstance();
            if(cal.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) 
                    && cal.get(Calendar.MONTH) == cal2.get(Calendar.MONTH)
                    && cal.get(Calendar.DATE) == cal2.get(Calendar.DATE)) {
                return true;
            } else {
                return false;
            }
        }
    }

    public void setInputDate(Date inputDate) {
        this.inputDate = inputDate;
    }

    public String getNumberDocument() {
        return numberDocument;
    }

    public void setNumberDocument(String numberDocument) {
        this.numberDocument = numberDocument;
    }

    public String getTypeDocument() {
        return typeDocument;
    }

    public void setTypeDocument(String typeDocument) {
        this.typeDocument = typeDocument;
    }

    public String getTypeMoney() {
        return typeMoney;
    }

    public void setTypeMoney(String typeMoney) {
        this.typeMoney = typeMoney;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Double getSubTotal() {
        
        return subTotal;
    }

    public void setSubTotal(Double subTotal) {
        this.subTotal = subTotal;
    }

    public Double getTotal() {
        return VarUtil.roundDouble(total, 2);
    }

    public void calculator() {
        subTotal = 0.0;
        if (detailInputDocumentList != null) {
            for (DetailInputDocument detailInputDocument : detailInputDocumentList) {
                subTotal = subTotal + detailInputDocument.getSubTotal();
            }
        }
        total = 0.0;
        total = ((igv / 100.0) * getSubTotal()) + getSubTotal();
    }

    public void setTotal(Double total) {
        this.total = total;
    }

    public Double getIgv() {
        return igv;
    }

    public void setIgv(Double igv) {
        this.igv = igv;
    }

    public Employee getEmployeeId() {
        return employeeId;
    }

    public void setEmployeeId(Employee employeeId) {
        this.employeeId = employeeId;
    }

    public Provider getProviderId() {
        return providerId;
    }

    public void setProviderId(Provider providerId) {
        this.providerId = providerId;
    }

    @XmlTransient
    public List<AccountForPay> getAccountForPayList() {
        return accountForPayList;
    }

    public void setAccountForPayList(List<AccountForPay> accountForPayList) {
        this.accountForPayList = accountForPayList;
    }

    @XmlTransient
    public List<DetailInputDocument> getDetailInputDocumentList() {
        return detailInputDocumentList;
    }

    public void setDetailInputDocumentList(List<DetailInputDocument> detailInputDocumentList) {
        this.detailInputDocumentList = detailInputDocumentList;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof InputDocument)) {
            return false;
        }
        InputDocument other = (InputDocument) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.alxbrendmaquinarias.model.InputDocument[ id=" + id + " ]";
    }

}
